import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/live/qkd_player_logic.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/view/live/items/gift_effect_view.dart';
import 'package:sports_new/view/live/items/live_expert_info.dart';
import 'package:sports_new/view/live/items/live_message_list.dart';
import 'package:sports_new/view/live/items/qkd_player.dart';
import 'package:sports_new/view/live/items/vertical_panel.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';
import '../../controller/live/live_normal_logic.dart';
import '../../widgets/card.dart';
import '../../widgets/share/share_sheet.dart';
import 'items/live_share_append.dart';

class LiveNormalPage extends StatefulWidget {
  const LiveNormalPage({super.key});

  @override
  State<LiveNormalPage> createState() => _LiveNormalPageState();
}

class _LiveNormalPageState extends State<LiveNormalPage> {
  final logic = Get.put(LiveNormalLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LiveNormalLogic>(
      builder: (logic) {
        return AnnotatedRegion<SystemUiOverlayStyle>(
          value: const SystemUiOverlayStyle(
            statusBarBrightness: Brightness.light,
            statusBarIconBrightness: Brightness.light
          ),
          child: Scaffold(
            backgroundColor: Colors.black,
            body: LoadingWidget<bool>(
              isLoading: logic.liveDetail == null,
              loading: Container(),
              data: false,
              child: () => Stack(
                alignment: AlignmentDirectional.topCenter,
                children: [
                  Stack(
                    children: [
                      QkdPlayer(logic: logic.playerLogic),
                      Positioned.fill(
                        child: VerticalPanel(logic: logic)
                      )
                    ],
                  ),
                  if(logic.liveDetail != null && !logic.waitStart && logic.liveDetail?.status == 1 && logic.playerLogic.status != PlayStatus.completion)
                  Positioned.fill(
                    child: LiveMessageList(
                      liveDetail: logic.liveDetail,
                      scene: MessageScene.normal,
                    ),
                  ),
                  const GiftEffect(),
                  Positioned(
                    top: 0,left: 0,right: 0,
                    child: topBar()
                  ),
                ],
              ),
            ),
          ),
        );
      }
    );
  }

  Widget topBar(){
    return SafeArea(
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 4),
        child: Row(
          children: [
            // if(!logic.waitStart)
            LiveExpertInfo.normal(
              liveDetail: logic.liveDetail,
              onFocusChanged: (focus){
                logic.liveDetail?.isFocus = focus?1:0;
                logic.getData();
              },
            ),
            const Spacer(),
            Block(
              onTap: () async{
                Utils.onEvent('spzb',params: {'spzb':2});
                Get.bottomSheet(
                  ShareSheet(fromLive: true,append: LiveShareAppend(liveId: logic.liveId??0)),
                  settings: RouteSettings(
                    arguments: ShareEntity(
                      title: "【直播】这个专家超厉害，快来围观TA的直播",
                      content: "免费比赛分析，尽在球客岛App",
                    )
                  )
                );
              },
              cardStyle: const CardStyle(
                color: Colours.transparent,
                padding: EdgeInsets.symmetric(horizontal: 8,vertical: 10),
              ),
              child: Image.asset(
                Utils.getImgPath("live_share.png"),
                color: Colours.white,
                width: 24,
                height: 24,
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 8,vertical: 10),
              child: Image.asset(
                Utils.getImgPath("close.png"),
                width: 24,color: Colours.white
              ),
            ).tap(logic.exit),
            Container(width: 16),
          ]
        )
      )
    );
  }

}